package cn.jaye.fabric.common.util;

import cn.jaye.fabric.common.model.ResultBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.jaye.fabric.common.constant.ExceptionEnum;

/**
 * Created by Jaye on 2017/10/18.
 */
public class ResultUtil {

    /**
     * 返回成功，传入返回体具体出參
     * @param object
     * @return
     */
    public static String success(Object object){
        ResultBody result = new ResultBody();
        result.setCode(1000);
        result.setMsg("success");
        result.setData(object);
        return JSON.toJSONString(result, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 提供给部分不需要出參的接口
     * @return
     */
    public static String success(){
        return success(null);
    }

    /**
     * 自定义错误信息
     * @param code
     * @param msg
     * @return
     */
    public static String error(Integer code,String msg){
        ResultBody result = new ResultBody();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return JSON.toJSONString(result,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 返回异常信息，在已知的范围内
     * @param exceptionEnum
     * @return
     */
    public static String error(ExceptionEnum exceptionEnum){
        ResultBody result = new ResultBody();
        result.setCode(exceptionEnum.getCode());
        result.setMsg(exceptionEnum.getMsg());
        result.setData(null);
        return JSON.toJSONString(result,SerializerFeature.WriteMapNullValue);
    }
}